import Processor.Converter as Converter
import Plugin.blur.controller as Controller

Window = None  # 主窗体
BlurList = None  # 滤波器列表


def newBlur(action):
    window = Window  # 主窗体
    layer = window.currentLayer  # 获取当前图层句柄
    if layer:
        image = layer.image  # 当前图层QImage句柄
        mat = Converter.QImage2Numpy(image)  # 将QImage对象转化为numpy.array 对象
        blur = action.blur
        if 'controller' in blur:
            controller = blur['controller'](window, mat)
            controller.layer = layer
            controller.updateSlot()  # 初始更新
            if controller.exec_():
                pass


def init(window):
    """
    初始化
    :param window: 主窗体
    :return:
    """
    global Window
    global BlurList
    Window = window
    BlurList = [
        {
            'name': '均值滤波',
            'controller': Controller.AvgBlurController,
        },
        {
            'name': '中值滤波',
            'controller': Controller.MedianBlurController,
        },
        {
            'name': '高斯滤波',
            'controller': Controller.GaussianBlurController,
        },
        {
            'name': '双边滤波',
            'controller': Controller.BilateralFilterController,
        },
        {
            'name': '高反差保留',
            'controller': Controller.HighPassBlurController,
        },
        {
            'name': '图像锐化',
            'controller': Controller.SharpnessController,
        },
        {
            'name': '边缘检测',
            'controller': Controller.CannyController,
        },
    ]

    __menu()    # 创建菜单栏


def __menu():
    """
    将插件入口连接到菜单栏，这样，点击菜单栏按钮时，能使用本插件功能
    :return:
    """
    window = Window
    blurs = BlurList
    menuBar = window.menuBar()    # 菜单栏句柄
    menu_filter = menuBar.menu_filter   # 滤镜栏句柄
    menu_filter.addSeparator()
    for each in blurs:
        action = menuBar.newAction(each['name'], menuBar)  # 新建菜单栏选项
        action.funcArg = True
        action.func = newBlur   # 当按钮出发时，运行 newBlur 方法
        action.blur = each
        menu_filter.addAction(action)   # 把选项添加到 滤波 分类
    menu_filter.addSeparator()
